local _G = getfenv(0)
local strformat, strfind = string.format, string.find
local temp, temp2, temp3
local _,i
local GameTooltip = _G.GameTooltip;
local select = select;
local GetItemInfo = GetItemInfo;
local GetItemQualityColor = GetItemQualityColor;
local UnitSex = UnitSex;

local L = IceLocale;
local inspectName;
local inspectList = {};
local targetlinenum
local found, truenum

local unit
local bplayer, bdead, tapped
local tip, text, levelline, temp, temp2
local unitrace, unitCreatureType
local guild

local mouseTarget

function Icetip:RefreshMouseOverTarget(elapsed)
	--[[
	目前bug:
	当目标有了目标的目标之后 OnUpdate就无法工作
	]]
	
	if not IcetipDB.DisTarget or not targetlinenum then return end
	
	text = _G["GameTooltipTextLeft"..targetlinenum]
	if (not text) then return end
	
	tip, temp, temp2 = nil, nil, nil
	if (not UnitExists(Icetip.unittarget)) then
		mouseTarget = nil
		text:SetText()
		GameTooltip:Show()
	else
		name = UnitName(Icetip.unittarget)--获取目标名字
		if (name ~= mouseTarget) then
			mouseTarget = name or UNKNOWNOBJECT
			tip = strformat("|cffFFFF00%s: |r", L.TARGET)--显示[ 当前目标
			
			if UnitIsUnit(Icetip.unittarget, "player") then
				tip = strformat("%s |c00ff0000 %s |r", tip, L.YOU)
			elseif UnitIsUnit(Icetip.unittarget, Icetip.unit) then
				tip = strformat("%s |cffFFFFFF %s |r", tip, L.SELF)
			elseif UnitIsPlayer(Icetip.unittarget) then
				temp, temp2 = UnitClass(Icetip.unittarget)--获取职业信息
				if UnitIsEnemy(Icetip.unittarget, "player") then
					tip = strformat("%s |cffFF0000 %s |r |cff%s(%s)|r",tip, mouseTarget, (ClassColors[(temp2 or "")]), temp)
				else
					tip = strformat("%s |cffFFFFFF %s |r |cff%s(%s)|r",tip, mouseTarget, (ClassColors[(temp2 or "")]), temp)
				end
			else
				tip = strformat("%s |cffFFFF00 %s |r",tip, mouseTarget)
			end
		tip = strformat("%s |cffFFFF00|r", tip)
		text:SetText(tip)
		GameTooltip:Show()
		end
	end
end


function Icetip:OnMouseOverUnit(name, unit)
	--获取鼠标指定人物的类型
	if unit == "npc" then unit = "mouseover" end

	bplayer = UnitIsPlayer(unit)
	name = name or UnitName(unit)

	--死尸
	bdead = UnitHealth(unit) <= 0 and (not bplayer or UnitIsDeadOrGhost(unit))
	tapped = UnitIsTapped(unit) and (not UnitIsTappedByPlayer(unit))

	--faction
	reaction = UnitReaction(unit,"player")

	--提示背景颜色设置
	Icetip:setBackdropColor(unit)

	--获取血量
	--能量值
	
	hp, maxhp = UnitHealth(unit), UnitHealthMax(unit)
	Icetip:Healbar_OnUpdate(hp, maxhp)

	powerType = UnitPowerType(unit)
	mp, maxmp = UnitMana(unit), UnitManaMax(unit)

	Icetip:PowerBar_OnUpdate(powerType, mp, maxmp)
---------------------------[[******************]]----------------------
	tip, text, levelline, found, temp, temp2, temp3 = nil
	--进行改造
	--首先获取tooltip 行数
	truenum = GameTooltip:NumLines()
	--从2开始
	for i = 2, truenum do
		text = _G[GameTooltip:GetName().."TextLeft"..i]--将从2~xx的table导入到text下
		tip = text:GetText()--获取文字信息
		--使用GameTooltip:ClearLines
		--GameTooltip:ClearLines();
		--Icetip:Debug(tip);
		if tip then
			--等级
			if (not levelline) and strfind(tip, LEVEL) then
				levelline = i
			elseif (tip == PVP) then
				text:SetText()
				found = true
			elseif (tip == TAMEABLE) then
				text:SetText(strformat("|cff00FF00%s|r", tip))
			elseif (tip == NOT_TAMEABLE) then
				text:SetText(strformat("|cffFF6035%s|r", tip))
			else
				otherInfo = tip;
			end
		end
	end

		--插入目标行
	if IcetipDB.DisTarget then
		targetlinenum = truenum
		if not found then
			GameTooltip:AddLine("--------")
			targetlinenum = targetlinenum + 1
		end
		text = _G["GameTooltipTextLeft"..targetlinenum]
		if text then
			text:SetText()
		else
			targetlinenum = nil
		end
	end
	--天赋
	if IcetipDB.Talented then
		name = UnitName(unit)
		if not inspectList[name] then
			inspectList[name] = {};
		end
		if not inspectList[name].report then
			if UnitCanAttack("player", unit) then
				inspectList[name].report = 1
			else
				inspectList[name].report = 0
			end
		end
		if UnitIsVisible(unit) and UnitIsPlayer(unit) and UnitIsConnected(unit) then
			if inspectList[name].talent then
				GameTooltip:AddLine(inspectList[name].talent);

			elseif not inspectName then
				inspectName = name
				if not inspectList[name].class then
					inspectList[name].class,inspectList[name].eClass = UnitClass(unit)
				end
				if not inspectList[name].race then
					inspectList[name].race = UnitRace(unit)
				end
				if not inspectList[name].raceColor then
					if UnitCanAttack("player", unit) then
						inspectList[name].raceColor = "|cffc81e00"
					else
						inspectList[name].raceColor = "|cff1ec800"
					end
				end
				lastInspectTime = time();
				NotifyInspect(unit)
			elseif (time() - lastInspectTime) > 2 then
				inspectName = nil
			end
		end
	end
	
--等级行改写
	if levelline then
		--等级,如果目标死亡则显示尸体
		temp = UnitLevel(unit)
		temp2 = ""
		if bdead then
			if temp > 0 then
				temp2 = _G.LEVEL..(strformat(" |cff888888%d %s|r", temp, CORPSE))
			else--?? 尸体
				temp2 = _G.LEVEL..(strformat(" |cff888888?? %s|r", CORPSE))
			end
		elseif (temp > 0) then
			--等级颜色
			if UnitCanAttack("player", unit) or UnitCanAttack(unit,"player") then
				temp2 = _G.LEVEL..(strformat(" |cff%s%d|r", Icetip:GetDifficultyColor(temp), temp))
			else
			--普通颜色
				temp2 = _G.LEVEL..strformat(" |cff3377CC%d|r",temp)
			end
		else
			temp2 = _G.LEVEL..(strformat(" |cffFF0000 ??|r"))
		end

		--种族 职业/ 人物类型/ 宠物种类
		unitrace = UnitRace(unit)
		unitCreatureType = UnitCreatureType(unit)

		if unitrace and bplayer then
			if UnitFactionGroup(unit) == UnitFactionGroup("player") then
				temp = "00FF33"
			else
				temp = "FF3300"
			end

			temp2 = strformat("%s |cff%s%s|r", temp2, temp, unitrace)
			--职业
			_, temp = UnitClass(unit)
			if IcetipDB.DisClass then
				Icetip:SetClassFlag(temp)
			end			
			--职业着色
			temp = ClassColors[(temp or "")]
			temp2 = strformat("%s |cff%s%s|r", temp2, temp, _)
		elseif UnitPlayerControlled(unit) then
			temp2 = strformat("%s %s", temp2,(UnitCreatureFamily(unit) or unitCreatureType or ""))
		elseif unitCreatureType then
			if IcetipDB.DisFaction and reaction and reaction > 4 then
				temp2 = strformat("%s |cffFFFFFF%s|r (%s)", temp2, unitCreatureType, Icetip:GetUnitFaction(unit, reaction))
			elseif unitCreatureType == L.SPEC then--未指定
				temp2 = strformat("%s %s",temp2, _G.UNKNOWN)
			else
				temp2 = strformat("%s %s", temp2, unitCreatureType)
			end
		else
			temp2 = strformat("%s %s",temp2, _G.UKNOWNBEING)
		end
		tip = temp2

		--信息
		temp = nil
		temp2 = ""
		
		if bplayer then
			temp2 = "";--strformat(" (%s)", _G.PLAYER)
		elseif not UnitPlayerControlled(unit) then
			temp = UnitClassification(unit)
			if temp and temp ~= "normal" and UnitHealth(unit) > 0 then
				if temp == "elite" then
					temp2 = strformat("|cffFFFF33(%s)|r", _G.ELITE)
				elseif temp == "worldboss" then
					temp2 = strformat("|cffFF0000(%s)|r", _G.BOSS)
				elseif temp == "rare" then
					temp2 = strformat("|cffFF66FF(%s)|r", L.Rare)
				elseif temp == "rareelite" then
					temp2 = strformat("|cffFFAAFF(%s %s)|r",L.Rare, _G.ELITE)
				else
					temp2 = strformat("(%s)", temp)
				end
			end
		end
		_G["GameTooltipTextLeft"..levelline]:SetText( strformat("%s%s", tip, temp2))
	end

	--军衔显示 使用UnitPVPName(unit) 获取pvprank PlayerName
	if bplayer then
		if IcetipDB.DisClass then
			tip = "    ";
		else
			tip = "";
		end
		
		if IcetipDB.SexName then
			temp3 = nil
			temp3 = UnitSex("mouseover")
			tmprank = UnitLevel("mouseover")
			if tmprank < 70 then
				if temp3 == 2 then
					temp3 = L.NickName1;
				elseif temp3 == 3 then
					temp3 = L.NickName2
				end			
			else
				if temp3 == 2 then
					temp3 = L.NickName3
				elseif temp3 == 3 then
					temp3 = L.NickName4
				end
			end
		else
			temp3 = ""
		end

		if not IcetipDB.DisPvpRank then
			GameTooltipTextLeft1:SetText(strformat("%s%s %s",tip, name, temp3))
		else
			GameTooltipTextLeft1:SetText(strformat("%s%s %s", tip, (UnitPVPName(unit) or name), temp3))
		end
	end
	
--GUILD
	tip = nil
	guild, guildrank = GetGuildInfo(unit)
	if bplayer then
		if guild then
			tip = "< "..guild.." > - "..guildrank
		end
		_, temp = UnitName(unit)
		if (IcetipDB.Server) and (temp or tip) then
			if (temp and tip) then
				temp2 = " @ "
			else
				temp2 = ""
			end
			tip = strformat("%s|cffFFAA50%s%s|r", tip or "", temp2, temp or "")
		end
		if tip then
			if guild then
				GameTooltipTextLeft2:SetText(tip)
			elseif levelline == 2 then
				temp3 = GameTooltip:NumLines()
				for i = temp3, 2, -1 do
					_G["GameTooltipTextLeft"..i+1]:SetText(_G["GameTooltipTextLeft"..i]:GetText())
				end
				GameTooltipTextLeft2:SetText(tip)
				if targetlinenum then
					targetlinenum = targetlinenum + 1
				end
			end
		end
	end

		--区域显示
	if not IcetipDB.DisTarget and (UnitInParty(unit) or UnitInRaid(unit)) then
		if otherInfo ~= guild then
			if otherInfo == PLAYER_OFFLINE then
				tip = strformat("|cff808080%s|r", otherInfo);
			else
				tip = strformat("|cffffffff%s|r", otherInfo);
			end
			GameTooltip:AddLine(tip);
		end
	end

---color
 --给第一行Unit字体上色
	r,g,b = Icetip:UnitColor(unit,bdead,tapped, reaction)
	GameTooltipTextLeft1:SetTextColor(r,g,b)
	
	--第二行
	if tip or (levelline or levelline > 2) then
		if bdead or tapped then
			GameTooltipTextLeft2:SetTextColor(0.55,0.55,0.55)
		else
			GameTooltipTextLeft2:SetTextColor(r*0.86,g*0.86,b*0.86)
		end
	end
	
	if bplayer then
		if guild == GetGuildInfo("player") then
			GameTooltipTextLeft2:SetTextColor(0.9,0.45,0.7)
		else
			GameTooltipTextLeft2:SetTextColor(0.8,0.8,0.8)
		end
	end
	
	GameTooltip:Show()
	return unit
end

function Icetip_TalentShow()
	if inspectName then
		local name = inspectName
		inspectName = nil
		--配点名称及点数
		local name1,_,point1 = GetTalentTabInfo(1,true)
		local name2,_,point2 = GetTalentTabInfo(2,true)
		local name3,_,point3 = GetTalentTabInfo(3,true)
		local color1, color2, color3 = Icetip_talentColor(point1, 61),Icetip_talentColor(point2,61),Icetip_talentColor(point3,61)
		local talent_name, talent_text = getTalentSpecName({name1,name2,name3}, {point1,point2,point3},{color1, color2, color3} );
		if not inspectList[name] or not inspectList[name].talent then
			inspectList[name].talent = talent_name
			inspectList[name].text = talent_text
		end
			--判断是否有目标
		if IcetipDB.Talented and UnitExists("mouseover") and UnitName("mouseover") == name then
			GameTooltip:AddLine(talent_name);
			GameTooltip:Show();
		end
	end
end

Icetip.TalentBlock = {
	Enabled = true,
	RequestTypes = {"Event"},
	Events = {"INSPECT_TALENT_READY"},
	Commands = {
		{Function = Icetip_TalentShow},
	},
}

Icetip:Add(EVA.Block:New(Icetip.TalentBlock));